package com.practice.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorServiceMain {

	public static void main(String[] args) {
		ExecutorService service = Executors.newFixedThreadPool(4);
		for (int i = 0; i < 10; i++) {
			service.execute(new ExecutorCommand(i));
		}
		System.out.println("Submit finish");
		service.shutdown();
		System.out.println("Shut down finish.");
	}
}

class ExecutorCommand implements Runnable {
	private int index;

	public ExecutorCommand(int i) {
		this.index = i;
	}

	@Override
	public void run() {
		try {
			System.out.println("[" + this.index + "] start...");
			Thread.sleep((int) (Math.random() * 1000));
			System.out.println("[" + this.index + "] end.");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
